package net.thelostlands.thelostteleport;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User;
import com.earth2me.essentials.api.IEssentials;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;

public class TeleportPlayer implements CommandExecutor {

	public static TheLostTeleport plugin;
	public static Towny towny;
	private final transient IEssentials ess;

	@Override
	public boolean onCommand(CommandSender sender, Command command,
			String label, String[] args) {

		towny = (Towny) plugin.getServer().getPluginManager()
				.getPlugin("Towny");

		try {
			Player player = plugin.getServer().getPlayer(
					((Player) sender).getName());
			Resident resident = towny.getTownyUniverse().getResident(
					player.getName());
			Nation nation = resident.getTown().getNation();

			BufferedReader br = readFile();

			String firstLine = br.readLine();
			if (firstLine == "shuffled") {
				
				warpPlayer(br,player,nation);
				
			} else if (firstLine == "holder") {
				String holder;
				holder = br.readLine();
				br.close();
				File file = new File("plugins/TheLostTeleport.txt");
				file.delete();
				BufferedWriter out = new BufferedWriter(new FileWriter(
						"plugins/TheLostTeleport.txt"));
				out.write("shuffled");
				out.newLine();
				out.write(holder);

				List<Nation> nations = towny.getTownyUniverse().getNations();
				Collections.shuffle(nations);
				Iterator<Nation> iterator = nations.iterator();
				while (iterator.hasNext()) {
					Nation current = iterator.next();
					if (current.getName() != holder) {
						out.newLine();
						out.write(current.getName());
					}
				}

				out.close();
				
				br = readFile();
				warpPlayer(br,player,nation);
			
			} else {

			}

		} catch (Exception e) {

		}
		return true;
	}

	public BufferedReader readFile() {
		BufferedReader br = null;
		try {
			File file = new File("plugins/TheLostTeleport.txt");
			FileInputStream fstream = new FileInputStream(
					file.getAbsolutePath());
			DataInputStream in = new DataInputStream(fstream);
			br = new BufferedReader(new InputStreamReader(in));

		} catch (Exception e) {

		}
		return br;
	}

	public void warpPlayer(BufferedReader br, Player player, Nation nation) {
		try {
			String strLine;
			String shuffled[];
			shuffled = new String[countLines("plugins/TheLostTeleport.txt")];
			int i = 0;
			while ((strLine = br.readLine()) != null) {
				if (i > 0) {
					shuffled[i] = strLine;
				}
				i = i + 1;
			}

			String warp;
			for (i = 0; i < shuffled.length; i++) {
				if (shuffled[i] == nation.getName()) {
					warp = "warp_" + i;
					break;
				}
			}
			
			User user = (User) player;
			Trade trade = new Trade(0,ess);
			user.getTeleport().warp(warp, trade, TeleportCause.COMMAND);
			
		} catch (Exception e) {

		}
	}

	public int countLines(String filename) throws IOException {
		InputStream is = new BufferedInputStream(new FileInputStream(filename));
		try {
			byte[] c = new byte[1024];
			int count = 0;
			int readChars = 0;
			while ((readChars = is.read(c)) != -1) {
				for (int i = 0; i < readChars; ++i) {
					if (c[i] == '\n')
						++count;
				}
			}
			return count;
		} finally {
			is.close();
		}
	}

}
